import com.bookshare.dao.base.BookMapper;
import com.bookshare.dao.entity.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * Created by yujia on 16/3/21.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring-mybatis.xml"})
public class InitBookData {
    @Autowired
    BookMapper bookMapper;

    @Test
    public void initBookData() {
        String[] classification = {"外语", "教育", "文学", "生活", "社科", "经管", "艺术", "计算机"};
        String[] language = {"多语种", "中文", "英语", "德语", "俄语", "日语"};
        String[] publisher = {"科学出版社", "高等教育出版社", "机械工业出版社",
                "电子工业出版社", "清华大学出版社", "北京大学出版社", "中国人民大学出版社",
                "复旦大学出版社", "上海交通大学出版社", "浙江大学出版社"};
        String[] bookname = {"梦里花落知多少", "文海拾贝", "青春阴晴表", "学院墙壁记事", "浮生取义",
                "你终将爱我", "回忆未来", "梅雨之夕", "孤独书", "才性与玄理", "馨香一缕寄云边", "归来没有统帅",
                "上帝只给了他一只老鼠", "浅轻之夏", "时针转动十次", "云城", "天使之心", "心与心晴", "西潮",
                "21克倾城之恋", "你叫，什么", "走过去，走回来", "鬓角留过你的唇", "一个你和一个我", "当我站在你面前"};
        String[] author = {"赵媛", "钱湘怡", "孙宁宁", "李安", "闻和泰", "东方静", "赫连康"};

        for (int i = 0; i < 4; i++) {
            Book book = new Book();
            book.setBookIsbn(getCode(13, 0));
            book.setBookName(getOne(bookname));
            book.setBookAuthor(getOne(author));
            book.setBookLanguage(getOne(language));
            book.setBookPublisher(getOne(publisher));
            if(i<88){
                book.setBookClassification(classification[0]);
            }
            if(i>=88 && i<123){
                book.setBookClassification(classification[1]);
            }
            if(i>=123 &&i<300){
                book.setBookClassification(classification[2]);
            }
            if(i>=300 &&i<444){
                book.setBookClassification(classification[3]);
            }
            if(i>=444 &&i<594){
                book.setBookClassification(classification[4]);
            }
            if(i>=594 &&i<600){
                book.setBookClassification(classification[5]);
            }
            if(i>=600 &&i<743){
                book.setBookClassification(classification[6]);
            }
            if(i>=743 &&i<800){
                book.setBookClassification(classification[7]);
            }
            bookMapper.insertSelective(book);
        }

    }

    public String getOne(String[] tmp) {
        Random random = new Random();
        random.setSeed(new Date().getTime());
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(tmp[random.nextInt(tmp.length)]);
        return stringBuffer.toString();
    }

    public String getCode(int passLength, int type) {
        StringBuffer buffer = null;
        StringBuffer sb = new StringBuffer();
        Random r = new Random();
        r.setSeed(new Date().getTime());
        switch (type) {
            case 0:
                buffer = new StringBuffer("0123456789");
                break;
            case 1:
                buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
                break;
            case 2:
                buffer = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
                break;
            case 3:
                buffer = new StringBuffer(
                        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
                break;
            case 4:
                buffer = new StringBuffer(
                        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
                sb.append(buffer.charAt(r.nextInt(buffer.length() - 10)));
                passLength -= 1;
                break;
            case 5:
                String s = UUID.randomUUID().toString();
                sb.append(s.substring(0, 8) + s.substring(9, 13)
                        + s.substring(14, 18) + s.substring(19, 23)
                        + s.substring(24));
        }

        if (type != 5) {
            int range = buffer.length();
            for (int i = 0; i < passLength; ++i) {
                sb.append(buffer.charAt(r.nextInt(range)));
            }
        }
        return sb.toString();
    }
}
